<?php
require_once (HOME . '/include/base/BackendController.php');
/**
 *
 *
 * 权限控制器
 *
 * @author silenus
 * 
 * 
 */
class privilegeManager extends BackendController {
	
	/**
	 */
	private $mod_privilege;
	private $privilegeList;
	function __construct() {
		parent::__construct ();
		$this->privilegeManager ();
	}
	function privilegeManager() {
		$this->mod_privilege = spClass ( 'm_privilege' );
	} // end construct
	function index() {
		if (! IS_POST) {
			/**
			 * $privilege_List = array(
			 * 'name' 权限名称
			 * 'emp_allow'=>array('id','name')
			 * 'emp_disallow'=>array('id','name')
			 * 'dep_allow'=>array('id','name')
			 * 'dep_disallow'=>array('id','name')
			 * )
			 */
			$privilege_List = array ();
			$mod_rst = $this->mod_privilege->spLinker ()->findAll ();
// 			dump ( $mod_rst );
			// 			foreach ( $mod_rst as $mod_key => $mod_value ) {
			// 				if (empty ( $mod_value ['auth_emp'] )) {
			// 					continue;
			// 				}
			// 				foreach ( $mod_value ['auth_emp'] as $auth_emp ) {
			// 					$temp_arr = array ();
			// 					if ($auth_emp ['is_allow'] && $auth_emp ['is_allow'] == 1) {
			// 						unset ( $auth_emp ['privilege_id'] );
			// 						$emp_arr ['is_allow'] = $auth_emp;
			// 						$realname = null;
			// 						foreach ( $mod_value ['employee'] as $name_arr ) {
			// 							if ($name_arr == $auth_emp ['employee_id'])
			// 								$realname = $name_arr ['realname'];
			// 						}
			// 						$emp_arr ['name'] = $realname;
			// 					} else if ($auth_emp ['is_allow'] && $auth_emp ['is_allow'] == 0) {
			

			// 					}
			// 				} //end second foreach
			// 			} //end first foreach
			foreach ( $mod_rst as $item ) {
				$emp = $this->_merge_for_auth ( $item ['auth_emp'], $item ['employee'], 'employee_id' );
				$dep = $this->_merge_for_auth ( $item ['auth_dep'], $item ['department'], 'department_id' );
// 				dump ( $test );
				$pid = $item['privilege_id'];
				$pname = $item ['name'];
				$this->privilegeList[$pid] = array('privilege_id'=>$pid,'name'=>$pname,'employee'=>$emp,'department'=>$dep);
			}
// 			dump($this->privilegeList);
			$this->assign('privilegeList', $this->privilegeList);
			////display page////
			$this->assign ( 'action', 'list' );
			$this->display ( 'privilegeManager.layout.html' );
		} else {
		}
	
	}
	private function _merge_for_auth($arrayList1, $arrayList2, $map_key) {
		$temp = array ('allow' => null, 'disallow' => null );
		foreach ( $arrayList1 as $v1 ) {
			if (array_key_exists ( $map_key, $v1 )) {
				foreach ( $arrayList2 as $v2 ) {
					if (array_key_exists ( $map_key, $v2 ) && $v2 [$map_key] == $v1 [$map_key]) {
						// 						array_push ( $temp, array_merge ( $v1, $v2 ) );
						$final_item = array_merge ( $v1, $v2 );
// 						var_dump($final_item);
						if ($final_item ['is_allow'] == '1' ||$final_item ['is_allow'] == 1) {
							$temp ['allow'][] = array_merge ( $v1, $v2 ) ;
						} elseif ($final_item ['is_allow'] == '0' || $final_item ['is_allow'] == 0) {
							$temp ['disallow'][] = array_merge ( $v1, $v2 ) ;
						}
					}
				}
			} else {
				continue;
			}
		}
		return $temp;
	}
	function add() {
		if (! IS_POST) { // 没有POST提交数据
			$this->assign ( 'action', 'add' );
			$this->display ( 'privilegeManager.layout.html' );
		} else if (IS_AJAX) { // AJAX提交
			$dep_id = $this->spArgs ( 'id' );
			$is_allow = $this->spArgs ( 'is_allow' );
			if ($dep_id && $is_allow) {
				$this->mod_department->spLinker ()->create ();
			}
		} else {
			// Do Nothing
		}
	}
	function get_pid() {
		$privilege_name = $this->spArgs ( 'privilege_name' );
		if (! empty ( $privilege_name )) {
			$pid = $this->mod_privilege->create ( array ('name' => $privilege_name ) );
			echo ( int ) $pid;
		}
	}
	function album() {
		$tree = array ();
		$mod_pcategory = spClass ( 'm_pcategory' );
		if ($pcategory_id = $this->spArgs ( 'album_id' )) {
			$result = $mod_pcategory->spLinker ()->findAll ( array ("parent_id" => $pcategory_id ) ); // 					'SUBSTRING(data.mime, 1, 5)' => 'image' 
		

		// dump ( $result );
		// json_encode($result);
		} else {
			$result = $mod_pcategory->spLinker ()->findAll ( 'parent_id = 0' );
		} // sql 查询
		if (empty ( $result )) {
			echo json_encode ( array ('error' => '下面没有数据了!!!' ) );
		} else {
			foreach ( $result as $i => $obj ) {
				$tree [$i] ['id'] = $obj ['pcategory_id'];
				$tree [$i] ['name'] = $obj ['name'];
				// $tree [$i] ['dep_name'] = $obj ['name'];
				$tree [$i] ['isParent'] = TRUE;
			
		// $tree [$i] ['department_id'] = "my HERO";
			}
			echo json_encode ( $tree );
		}
	}
	/**
	 * 文章访问限制
	 */
	function articleAccess() {
		if ((! IS_POST) && (! IS_AJAX)) {
		} elseif (! IS_AJAX) { // 普通post提交
			$auth_code = $this->spArgs ( "auth_code" );
			$employee_id = $this->spArgs ( "employee_id" );
			
			$request_data_id = $this->spArgs ( "request_data_id" );
			
			if (true == $this->checkAuth ( $auth_code, $employee_id )) {
				// 授权访问
			} else {
				// 无权访问
			}
		} elseif (IS_AJAX) { // ajax提交处理
			$type = isset ( $_REQUEST ['type'] ) ? $_REQUEST ['type'] : false;
			if ($type) {
				switch ($type) {
					case 'json' :
						$content = $this->spArgs ( 'content' );
						$content = json_decode ( $content );
						break;
					case 'xml' :
						break;
				}
			} // end if
		}
	}
	/**
	 * 
	 */
	function test(){
		
	}
	/**
	 * 文章内容访问限制
	 */
	function articleContentAccess() {
	}
	/**
	 * 下载限制
	 */
	function downAccess() {
	}
	/**
	 * data访问控制
	 */
	function dataAccess() {
	}
	
	/**
	 * 检查授权情况
	 */
	private function checkAuth($auth_code, $employee_id) {
		$result = spClass ( "m_privilege" )->checkAuth ( $auth_code, $employee_id );
		return $result;
	}
	
	/**
	 */
	private function parseRequest() {
	}
	/**
	 *
	 * @param array $arrayList
	 * @param array $map 数组数据与返回数据之间的映射 改变键值
	 */
	protected function _tree_change_key($arrayList, $map = array()) {
		$return_arr = array ();
		foreach ( $arrayList as $List_key => $List_value ) {
			foreach ( $map as $map_key => $map_value ) {
				if (is_string ( $map_key ) && array_key_exists ( $map_key, $List_value )) {
					$return_arr [$List_key] [$map_value] = $List_value [$map_key];
				} else if (is_numeric ( $map_key )) {
					$return_arr [$List_key] [$map_value] = $List_value [$map_value];
				} else {
					return false;
				}
			}
		}
		return $return_arr;
	}
	
	public function pList(){
		
		//TODO need to remove // 
// 		if(!IS_POST){
// 			echo json_decode(array('error' =>'not ajax!!! '));
// 		}else{
			$rst = $this->mod_privilege->findAll(NULL,NULL,'privilege_id , name');
// 			dump($rst) ;
			foreach ($rst as $item){
				$output = <<<SPAN
<span class='pList' pid='{$item['privilege_id']}'>{$item['name']}</span>
SPAN;
			echo $output;
			}
// 		}
	}
}